"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.classroomMiddleware = void 0;
const enums_1 = require("../../utils/enums");
const Classrooms_1 = require("../../lib/Classrooms");
const socket_message_1 = require("../../utils/socket_message");
const utils_1 = require("../../utils/utils");
const log4js_1 = require("log4js");
const Member_1 = require("../../lib/Member");
const logger = (0, log4js_1.getLogger)('socket/classroomMiddleware');
async function queryResidentGroup(app, owner) {
    let clientUtils = app.locals.clientUtils;
    if (!(owner === null || owner === void 0 ? void 0 : owner.axios)) {
        console.log('must have user online, can getCommunicationGroup');
        return;
    }
    const { code, data } = await owner.axios.getCommunicationGroup();
    if (+code === 0 && Array.isArray(data)) {
        for (const room of data) {
            if (!Classrooms_1.Classrooms.has(room.id)) {
                const classroom = Classrooms_1.Room.create({
                    creator: {
                        userId: room.createUserId,
                        username: '',
                    },
                    classId: room.id,
                    classname: room.distributionGroupName,
                    createTime: room.createTime,
                    classType: enums_1.RoomTypeEnums.RESIDENT,
                    chatMode: enums_1.ChatModeEnums.INTERCOM,
                    members: []
                });
                if (room.personId) {
                    const memberIds = room.personId.split(',');
                    const memberNames = room.personName.split(',');
                    const inviteMembers = memberIds.map((userId, index) => ({
                        userId,
                        username: memberNames[index]
                    }));
                    classroom.setInviteMembers(inviteMembers);
                }
                if (clientUtils) {
                    classroom.client = await clientUtils.getClient();
                    if (!classroom.client) {
                        return console.error('client was not fined!');
                    }
                    classroom.pipeline = await clientUtils.createMediaPipeline(classroom.client);
                }
                logger.debug(`常驻组 ${(0, utils_1.stringify)(classroom.json())} 加入成功！`);
                Classrooms_1.Classrooms.set(room.id, classroom);
            }
        }
    }
    return Classrooms_1.Classrooms.toArray();
}
async function classroomMiddleware(app, io) {
    const { socket } = app.locals;
    const { userId } = socket.handshake.query;
    queryResidentGroup(app, Member_1.Members.get(userId)).then(classroom => {
        socket.emit('classroom', classroom);
    });
    socket.on('classroom:refresh', function (cb) {
        const classroom = Classrooms_1.Classrooms.toArray();
        socket.emit('classroom', classroom);
    });
    socket.on('classroom:all', function (cb) {
        const classroom = Classrooms_1.Classrooms.toArray();
        cb && cb((0, socket_message_1.okMsg)('classroom:all', classroom));
    });
    socket.on('classroom:resident', function (data, cb) {
        const { classId, userId } = data;
        console.log('常驻房间: => ', classId, userId);
        if (data.userId === userId) {
            cb('ffffffffffffffff----');
        }
    });
    socket.on('classroom:temporary', function (data, cb) {
        const { classId, userId } = data;
        console.log('临时房间: => ', classId);
        if (data.userId === userId) {
            cb('ffffffffffffffff----');
        }
    });
}
exports.classroomMiddleware = classroomMiddleware;
